﻿var START_1 = 194;
var START_2 = 696;
var START_3 = 713;

var IMG_PATH = "http://yomoda-zborav-0.googlecode.com/svn/trunk/img/";
var MAX_SIZE = 30;
var DEF_SIZE = 20;
var LINE_SIZE = MAX_SIZE-1;
var PIECES = new Array(
	IMG_PATH + "feald.gif",
	IMG_PATH + "b1.gif",
	IMG_PATH + "b2.gif",
	IMG_PATH + "b3.gif",
	IMG_PATH + "wall.gif",
	IMG_PATH + "limit.gif"
);
var PIECES_ARRAY = new Array();

var TIMER;

window.onload = function()
{
	create_feald("result");
	create_torisetsu("result");
	start();
}
function create_feald(target_id)
{
	var frame = document.createElement("div");
	with(frame){
		id = "feald";
		with(style){
			width  = (MAX_SIZE*12) + "px";
//			height = (MAX_SIZE*12) + "px";
			margin = "0px auto";
		}
	}
	document.getElementById(target_id).appendChild(frame);
	return;
}
function create_controll(target_id)
{
	var frame = document.createElement("div");

	var reset_button = document.createElement("button");
	with(reset_button){
		innerHTML = "再起動";
		onclick = new Function('start();');
	}

	var controll_button = document.createElement("button");
	with(controll_button){
		innerHTML = "ストップ";
		onclick = new Function('controll(this);');
	}

	with(frame){
		appendChild(reset_button);
		appendChild(controll_button);
	}
	document.getElementById(target_id).appendChild(frame);
}
function create_torisetsu(target_id)
{
	var frame = document.createElement("div");
	frame.innerHTML
		=	'<ul style="text-align:left;">'
		+		'<li>'
		+			'<img src="' + PIECES[2] + '" / >は'
		+			'<img src="' + PIECES[1] + '" / >に、'
		+			'<img src="' + PIECES[3] + '" / >は'
		+			'<img src="' + PIECES[2] + '" / >に、'
		+			'<img src="' + PIECES[1] + '" / >は'
		+			'<img src="' + PIECES[3] + '" / >に上書きされ、結果的にグルグル追いかけあうようになります。'
		+		'</li>'
		+		'<li>'
		+			'<img src="' + PIECES[4] + '" / >は壁、'
		+			'<img src="' + PIECES[0] + '" / >は何もないセルです。'
		+		'</li>'
		+		'<li>'
		+			'盤面をクリックすると「壁」を作ることができます。作った壁を消すときは、もう一度クリックします。'
		+		'</li>'
		+	'</ul>'
	;
	document.getElementById(target_id).appendChild(frame);
}
function start(target_id)
{
	document.getElementById("feald").innerHTML = "";

	var matrix = new Array();
	var max = Math.pow(MAX_SIZE, 2);
	var lastline = MAX_SIZE*LINE_SIZE;

	//全体
	for(var i=0 ; i<max ; i++){
		var img_obj = document.createElement("img");
		var n = i<MAX_SIZE || i>=lastline || i%MAX_SIZE==0 || i%MAX_SIZE==LINE_SIZE ? 5 : 0;
		with(img_obj){
			src = PIECES[n];
			title = alt = "";
			with(style){
				width = "12px";
				height = "12px";
			}
		}
		PIECES_ARRAY[i] = img_obj;
	}

	//有効盤面
	for(var i=0 ; i<PIECES_ARRAY.length ; i++){
		var img_obj = PIECES_ARRAY[i];
		with(document.getElementById("feald")){
			appendChild(img_obj);
			with(img_obj){
				if(src==PIECES[0]){
					title = alt = "クリック！";
					onclick = new Function("change(this);");
					style.cursor = "crosshair";
				}
			}
			if(i%MAX_SIZE==LINE_SIZE){
				appendChild(document.createElement("br"));
			}
		}
	}

	//中枠
	var start = Math.floor((MAX_SIZE-DEF_SIZE)/2)*(MAX_SIZE+1);
	var end   = DEF_SIZE + start;
	var seek_y = max-start-end;
	var seek_x = DEF_SIZE-1;
	for(var i=start ; i<end ; i++){
		PIECES_ARRAY[i].src = PIECES[4];
		PIECES_ARRAY[i+seek_y].src = PIECES[4];
	}
	start += MAX_SIZE;
	end = start+seek_y-MAX_SIZE;
	for(var i=start ; i<end ; i+=MAX_SIZE){
		PIECES_ARRAY[i].src = PIECES[4];
		PIECES_ARRAY[i+seek_x].src = PIECES[4];
	}

	//初期位置
	PIECES_ARRAY[START_1].src = PIECES[1];
	PIECES_ARRAY[START_2].src = PIECES[2];
	PIECES_ARRAY[START_3].src = PIECES[3];

	if(TIMER){
		clearInterval(TIMER);
	}
	TIMER = setInterval("move()", 10);
	create_controll("feald");

	return;
}

var NowIndex = 1;
var Priolity = {
	1:2,
	2:3,
	3:1
};

function move()
{
	var enable_array = new Array();

	for(var i=0 ; i<1000 ; i++){
		var idx = rand(PIECES_ARRAY.length);
		if(PIECES_ARRAY[idx].src==PIECES[NowIndex]){
			enable_array = check_move(idx);
			if(enable_array.length>0){
				break;
			}
		}
	}
	if(enable_array.length>0){
		PIECES_ARRAY[enable_array[rand(enable_array.length)]].src = PIECES[NowIndex];
	}

	NowIndex++;
	if(NowIndex>3){
		NowIndex = 1;
	}
}
function check_move(idx)
{
	var enable_piece = Priolity[NowIndex];
	var enable_array = new Array();

	var up_idx    = idx-MAX_SIZE;
	var right_idx = idx+1       ;
	var left_idx  = idx-1       ;
	var down_idx  = idx+MAX_SIZE;

	var up_src    = PIECES_ARRAY[up_idx   ].src;
	var right_src = PIECES_ARRAY[right_idx].src;
	var left_src  = PIECES_ARRAY[left_idx ].src;
	var down_src  = PIECES_ARRAY[down_idx ].src;

	if(up_src==PIECES[0] || up_src==PIECES[enable_piece]){
		enable_array[enable_array.length] = up_idx;
	}
	if(right_src==PIECES[0] || right_src==PIECES[enable_piece]){
		enable_array[enable_array.length] = right_idx;
	}
	if(left_src==PIECES[0] || left_src==PIECES[enable_piece]){
		enable_array[enable_array.length] = left_idx;
	}
	if(down_src==PIECES[0] || down_src==PIECES[enable_piece]){
		enable_array[enable_array.length] = down_idx;
	}
	return enable_array;
}

function controll(controll_button)
{
	if(controll_button.innerHTML=="ストップ"){
		controll_button.innerHTML = "リジューム";
		clearInterval(TIMER);
	}else{
		controll_button.innerHTML = "ストップ";
		TIMER = setInterval("move()", 10);
	}
}


function rand(n)
{
	return Math.floor(Math.random()*n);
}
function change(obj)
{
	with(obj){
		src = src==PIECES[4] ? PIECES[0] : PIECES[4];
	}
}
